package cn.lecosa.spark.es.java;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import cn.lecosa.es.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/*public class QueryTest {
	private static TransportClient client = EsClient.eSClient();




	public static void main(String[] args) throws UnknownHostException {

//		matchAllQuery();
		*//* full text queries *//*
		matchQuery();
		multiMatchQuery();
		//commonTermQuery(client);
		*//* end of full text queries *//*
		*//* term query *//*
		//rangeQuery(client);
//		termQuery(client);
//		othersQuery(client);
		*//* end of term query*//*
		client.close();
	}

	public static void matchAllQuery() {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders.matchAllQuery();
		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()){
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();
	}

	public static void matchQuery( ) {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders.matchQuery("gid", "110000181");

		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()){
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();
	}

	public static void multiMatchQuery() {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders
				.multiMatchQuery("imsi-data","title", "body");

		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) {
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();
	}

	public static void commonTermQuery(Client client ) {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders
				.commonTermsQuery("title","imsi-data");

		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) {
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();

		//common terms query
	}

	public static void termQuery() {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders
				.termQuery("title","imsi-data");

//		QueryBuilder qb = QueryBuilders
//				.termsQuery("title","imsi-data","relevence");

		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
//				.addHighlightedField("title")
//				.setHighlighterPreTags("<span class='searchKey'>")
//				.setHighlighterPostTags("</san>")
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits()) {
			Map<String, HighlightField> result = hit.highlightFields();
			HighlightField highlightedSummary = result.get("title");
			Text[] titleTexts = highlightedSummary.getFragments();
			String allFragments = "";
			for (Text text: titleTexts){
				allFragments += text;
			}
			System.out.println(result);
			System.out.println(allFragments);
		}

		// on shutdown
		client.close();

		//common terms query
	}

	public static void rangeQuery(Client client ) {
		SearchResponse res = null;

		QueryBuilder qb = QueryBuilders
				.rangeQuery("like")
				.gte(5)
				.lt(7);


//		QueryBuilder qb = QueryBuilders
//				.rangeQuery("like")
//				.from(5)
//				.to(7)
//				.includeLower(true)
//				.includeUpper(false);


		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) {
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();

		//common terms query
	}

	public static void othersQuery(Client client ) {
		SearchResponse res = null;

		QueryBuilder qb = QueryBuilders.existsQuery("str");
		//QueryBuilder qb = QueryBuilders.prefixQuery("name", "prefix");
		//QueryBuilder qb = QueryBuilders.wildcardQuery("user", "k?mc*");
		//QueryBuilder qb = QueryBuilders.regexpQuery("user", "k.*y");
		//QueryBuilder qb = QueryBuilders.fuzzyQuery("name", "kimzhy");
		//QueryBuilder qb = QueryBuilders.typeQuery("my_type");
		//QueryBuilder qb = QueryBuilders.idsQuery("my_type","type2").addIds("1","2","5");



		res = client.prepareSearch("imsi4")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) {
			System.out.println(hit.getSourceAsString());
		}

		// on shutdown
		client.close();

		//common terms query
	}

}*/
